package abcy.cloud.authenticationserver.config.details.user;


import abcy.cloud.authenticationserver.dto.req.SysUserSelectReqDto;
import abcy.cloud.authenticationserver.dto.resp.SysLoginUserResp;
import abcy.cloud.authenticationserver.dto.resp.SysUserSelectRespDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserDetailsService implements CustomUserDetailsService{

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public UserDetails loadUserByUsername(String username, String source) throws UsernameNotFoundException {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
        formData.add("username", username);
        SysUserSelectReqDto sysUserSelectReqDto = new SysUserSelectReqDto();
        sysUserSelectReqDto.setUsername(username);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String urlStr = "http://localhost:" + 8888 + "/sys/sysuser/getUserInfoByUserName";
        /*Map<?,?> resultMap1 = (Map<?, ?>) restTemplate.exchange(urlStr, HttpMethod.POST,
                new HttpEntity<MultiValueMap<String, String>>(formData, headers), Map.class);*/
        HttpEntity<SysUserSelectReqDto> entity = new HttpEntity<>(sysUserSelectReqDto, headers);
        Map<?, ?> resultMap = restTemplate.exchange(urlStr, HttpMethod.POST,
                entity, Map.class).getBody();
        if (resultMap != null) {
            @SuppressWarnings("unchecked")
            //List<String> authOrity = (List<String>) user.get("authOrity");

            /*List<String> strList = new ArrayList<>();
            strList.add("sys_admin");
            authOrity = strList;*/
            //authOrity.add("sys_admin");
            String PASSWORD = resultMap.get("password").toString();
            List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
            grantedAuthorityList.add(new GrantedAuthority() {
                @Override
                public String getAuthority() {
                    return "abc";
                }
            });
            return new User(username, PASSWORD, grantedAuthorityList);
        } else {
            throw new UsernameNotFoundException("用户[" + username + "]不存在");
        }
    }
}
